home *** CD-ROM | disk | FTP | other *** search
- /* DisplayAudit.c */
- /*
- * DisplayAudit.c
- * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
- * Programmed by Martin Minow,
- * Internet: minow@apple.com
- * AppleLink: MINOW
- *
- * Edit History
- * 93.01.09 MM First public release
- * 93.01.14 MM Removed Think C stdio dependencies. Added Standard File dialog
- * with additional dialog items to allow defining the audit
- * selector.
- * 93.01.19 MM MPW \r is the wrong character. Force <return> on both MPW and
- * Think, because this is what the Mac file system requires for
- * end of line. (Change is in DisplayAuditFile.c). #if'ed out
- * some test calls. Also added error status logging for file I/O
- * errors.
- * 93.01.22 MM Added a preference file that remembers the last audit selector
- * parameter and window sizes. Also added error alerts. Moved
- * global parameters to DisplayAudit.h.
- * 93.07.09 MM Reformatted text. Many other changes:
- * -- Removed "SimpleTextWindow" -- it was far too simple,
- * replacing it with the Log Manager from DTS Catalog Peek.
- * -- Added "number of lines to save" to the file dialog (and
- * wherever else necessary). Also added number of audit records
- * to create.
- * -- Revised overall structure so that we can (eventually)
- * support multiple Audit records.
- * -- Removed fixed "number of audit cycles," replacing it with
- * the number of audit records we've created.
- * -- Write parameter record whenever it changes. This includes
- * changes in the window shape. Note: the parameter record will
- * reflect the last window you created. If you create two, only
- * the second will be in the parameter record. Actually, the
- * "mainline" has been extensively rewritten.
- *
- * This stand-alone application displays the data from a AuditRecord, writing it
- * to a file or the screen. It is not very intelligent.
- */
- #define EXTERN /* */
- #include "DisplayAudit.h"
-
- void ProcessAudit(void);
- void DisplayLogEntry(void);
- void DumpAuditEntry(
- register AuditPtr auditPtr,
- register AuditEntryPtr entryPtr
- );
-
- void InitMacintosh(void);
- void InitApplication(void);
- void EventLoop(void);
- void ProcessAudit(void);
- void DoMouseEvent(void);
- void DoCommand(
- WindowPtr theWindow,
- long menuChoice
- );
- void AdjustMenus(void);
- void AdjustEditMenu(
- Boolean isDeskAcc
- );
- void DeleteAllWindows(void);
-
- void
- main(void)
- {
- InitMacintosh();
- gDebugOnError = (Get1Resource('CODE', 0) == NULL); /* Think C hack */
- TRY {
- InitApplication();
- MakeDocumentWindow();
- while (gQuitNow == FALSE) {
- if (gUpdateMenusNeeded) {
- gUpdateMenusNeeded = FALSE;
- AdjustMenus();
- }
- EventLoop();
- ProcessAudit();
- if (gParameterUpdateNeeded)
- WriteAuditParameters();
- }
- }
- CATCH {
- ErrorAlert(STATUS, MESSAGE, TRUE); /* Fatal */
- NO_PROPAGATE; /* Nothing to propagate to! */
- }
- ENDTRY;
- DeleteAllWindows();
- if (gParameterFileRefNum != kNoParamFile) {
- CloseResFile(gParameterFileRefNum);
- gParameterFileRefNum = kNoParamFile;
- }
- ExitToShell();
- }
-
- /*
- * EventLoop runs the "main" event loop. It calls subroutines for anything
- * of particular interest, such as mouse-clicks.
- */
- void
- EventLoop(void)
- {
- long menuChoice;
- register WindowPtr theWindow;
- GrafPtr savePort;
- Boolean isActivating;
-
- WaitNextEvent(
- everyEvent,
- &EVENT,
- (gInForeground) ? kForegroundTicks : kBackgroundTicks,
- NULL
- );
- theWindow = FrontWindow();
- switch (EVENT.what) {
- case nullEvent:
- break;
- case keyDown:
- case autoKey:
- if ((EVENT.message & charCodeMask) == '.'
- && (EVENT.modifiers & cmdKey) != 0) {
- FlushEvents(keyDown | autoKey, 0);
- gQuitNow = TRUE;
- }
- else if ((EVENT.modifiers & cmdKey) != 0) {
- if (EVENT.what == keyDown) {
- menuChoice = MenuKey(EVENT.message & charCodeMask);
- if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
- DoCommand(theWindow, menuChoice);
- else if (IsOurWindow(theWindow))
- DoWindowKeyDown((DocumentPtr) theWindow);
- else {
- SysBeep(10);
- }
- }
- }
- else if (IsOurWindow(theWindow))
- DoWindowKeyDown((DocumentPtr) theWindow);
- else {
- SysBeep(10);
- }
- break;
- case mouseDown:
- DoMouseEvent();
- break;
- case updateEvt:
- theWindow = (WindowPtr) EVENT.message;
- GetPort(&savePort);
- SetPort(theWindow);
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- UpdateControls(theWindow, theWindow->visRgn);
- if (IsOurWindow(theWindow))
- UpdateDocumentWindow((DocumentPtr) theWindow);
- EndUpdate(theWindow);
- SetPort(savePort);
- break;
- case activateEvt:
- theWindow = (WindowPtr) EVENT.message;
- isActivating = ((EVENT. modifiers & activeFlag) != 0);
- goto activateEvent;
- break;
- case osEvt:
- switch (((unsigned long) EVENT.message) >> 24) {
- case mouseMovedMessage:
- break;
- case suspendResumeMessage:
- isActivating = ((EVENT.message & 0x01) != 0);
- activateEvent: if (isActivating) {
- /*
- * Activate this window. Note that activate events
- * define theWindow from the EventMessage, while
- * suspend/resume events use the pre-set FrontWindow.
- */
- SelectWindow(theWindow);
- SetPort(theWindow);
- (void) TEFromScrap(); /* Not really needed */
- if (IsOurWindow(theWindow)) {
- ActivateDocumentWindow(
- (DocumentPtr) theWindow,
- isActivating
- );
- }
- else {
- /*
- * Hmm, can it be a desk accessory window?
- */
- }
- gInForeground = isActivating;
- gUpdateMenusNeeded = TRUE;
- }
- break; /* Break from osEvt switch */
- }
- break; /* Break from event switch */
- }
- }
-
- /*
- * Look at all of the windows. If any are ours, drain its audit record.
- */
- void
- ProcessAudit(void)
- {
- register WindowPtr thisWindow;
- register WindowPeek nextWindow;
-
- thisWindow = FrontWindow();
- while (thisWindow != NULL) {
- nextWindow = ((WindowPeek) thisWindow)->nextWindow;
- if (IsOurWindow(thisWindow))
- ProcessAuditDocument((DocumentPtr) thisWindow);
- thisWindow = (WindowPtr) nextWindow;
- }
- }
-
-
- /*
- * DoMouseEvent: the user clicked on something. Handle application-wide
- * processing here, or call a DisplayAudit function for specific action.
- */
- void
- DoMouseEvent(void)
- {
- WindowPtr theWindow;
- short whichPart;
-
- whichPart = FindWindow(EVENT.where, &theWindow);
- if (theWindow == NULL)
- theWindow = FrontWindow();
- if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
- theWindow = FrontWindow();
- switch (whichPart) {
- case inDesk:
- break;
- case inMenuBar:
- InitCursor();
- DoCommand(theWindow, MenuSelect(EVENT.where));
- break;
- case inDrag:
- DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, EVENT.where)) {
- if (IsOurWindow(theWindow)) {
- DisposeDocumentWindow((DocumentPtr) theWindow);
- if (gOpenWindowCount <= 0)
- gQuitNow = TRUE;
- }
- else {
- SysBeep(10);
- }
- }
- break;
- case inZoomIn:
- case inZoomOut:
- if (IsOurWindow(theWindow)
- && TrackBox(theWindow, EVENT.where, whichPart)) {
- DoZoomWindow(theWindow, whichPart);
- goto resizeWindow;
- }
- break;
- case inGrow:
- if (IsOurWindow(theWindow)
- && DoGrowWindow(theWindow, kMinWindowWidth, kMinWindowHeight)) {
- resizeWindow: DecorateWindow((DocumentPtr) theWindow);
- gParameterUpdateNeeded = TRUE;
- }
- break;
- case inContent:
- if (theWindow != FrontWindow()) {
- SelectWindow(theWindow);
- SetPort(theWindow);
- }
- else if (IsOurWindow(theWindow))
- DoContentClick((DocumentPtr) theWindow);
- else {
- /* Nothing happens here */
- }
- break;
- default:
- break;
- }
- }
-
- /*
- * A menu choice.
- */
- void
- DoCommand(
- WindowPtr theWindow,
- long menuChoice
- )
- {
- short menuItem;
- Str255 menuText;
- GrafPtr savePort;
-
- menuItem = LoWord(menuChoice);
- switch (HiWord(menuChoice)) {
- case MENU_Apple:
- if (menuItem != kAppleAbout) {
- GetItem(gAppleMenu, menuItem, menuText);
- AdjustEditMenu(TRUE);
- GetPort(&savePort);
- OpenDeskAcc(menuText);
- SetPort(savePort);
- AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
- }
- break;
- case MENU_File:
- switch (menuItem) {
- case kFileNewWindow:
- MakeDocumentWindow();
- break;
- case kFileCloseWindow:
- if (IsOurWindow(theWindow)) {
- DisposeDocumentWindow((DocumentPtr) theWindow);
- if (gOpenWindowCount <= 0)
- gQuitNow = TRUE;
- }
- break;
- case kFileSaveAs:
- if (IsOurWindow(theWindow))
- DoDocumentSaveAs((DocumentPtr) theWindow);
- break;
- case kFileCloseFile:
- if (IsOurWindow(theWindow))
- DoDocumentCloseFile((DocumentPtr) theWindow);
- break;
- case kFileQuit:
- gQuitNow = TRUE;
- break;
- default:
- SysBeep(10);
- break;
- }
- break;
- case MENU_Edit:
- if (SystemEdit(menuItem - 1) == FALSE)
- SysBeep(10);
- break;
- }
- HiliteMenu(0);
- gUpdateMenusNeeded = TRUE;
- }
-
- void
- AdjustMenus(void)
- {
- register WindowPtr theWindow;
- register DocumentPtr documentPtr;
-
- theWindow = FrontWindow();
- EnableItem(gFileMenu, kFileQuit);
- EnableItem(gFileMenu, kFileNewWindow);
- if (IsOurWindow(theWindow)) {
- EnableItem(gFileMenu, kFileCloseWindow);
- documentPtr = (DocumentPtr) theWindow;
- if (DOC.logFileRefNum == 0) {
- EnableItem(gFileMenu, kFileSaveAs);
- DisableItem(gFileMenu, kFileCloseFile);
- }
- else {
- EnableItem(gFileMenu, kFileCloseFile);
- DisableItem(gFileMenu, kFileSaveAs);
- }
- CheckItem(gFileMenu, kFileSaveAs, DOC.logFileRefNum != 0);
- }
- AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
- }
-
- void
- AdjustEditMenu(
- Boolean isDeskAcc
- )
- {
- if (isDeskAcc) {
- EnableItem(gEditMenu, kEditUndo);
- EnableItem(gEditMenu, kEditCut);
- EnableItem(gEditMenu, kEditCopy);
- EnableItem(gEditMenu, kEditPaste);
- EnableItem(gEditMenu, kEditClear);
- }
- else {
- DisableItem(gEditMenu, kEditUndo);
- DisableItem(gEditMenu, kEditCut);
- DisableItem(gEditMenu, kEditCopy);
- DisableItem(gEditMenu, kEditPaste);
- DisableItem(gEditMenu, kEditClear);
- }
- }
-
- void
- DeleteAllWindows(void)
- {
- WindowPtr thisWindow;
- WindowPeek nextWindow;
-
- thisWindow = FrontWindow();
- while (thisWindow != NULL) {
- nextWindow = ((WindowPeek) thisWindow)->nextWindow;
- if (IsOurWindow(thisWindow))
- DisposeDocumentWindow((DocumentPtr) thisWindow);
- thisWindow = (WindowPtr) nextWindow;
- }
- }
-
- void
- InitMacintosh(void)
- {
- int i;
- long response;
-
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- HNoPurge((Handle) GetCursor(watchCursor));
- SetCursor(*GetCursor(watchCursor));
- for (i = 0; i < 3; i++)
- EventAvail(everyEvent, &EVENT);
- if (Gestalt(gestaltQuickdrawVersion, &response) == noErr
- && response >= gestalt8BitQD)
- gHasColorQuickDraw = TRUE;
-
- }
-
- void
- InitApplication(void)
- {
- Handle menuBarHdl;
-
- menuBarHdl = GetNewMBar(MBAR_MenuBar);
- FailNIL(menuBarHdl, kErrInitialization);
- SetMenuBar(menuBarHdl);
- gAppleMenu = GetMHandle(MENU_Apple);
- FailNIL(gAppleMenu, kErrInitialization);
- gFileMenu = GetMHandle(MENU_File);
- FailNIL(gFileMenu, kErrInitialization);
- gEditMenu = GetMHandle(MENU_Edit);
- FailNIL(gEditMenu, kErrInitialization);
- AddResMenu(GetMHandle(MENU_Apple), 'DRVR');
- DrawMenuBar();
- gFontMenu = GetMenu(MENU_Font);
- FailNIL(gFontMenu, kErrInitialization);
- AddResMenu(gFontMenu, 'FONT');
- gFontSizeMenu = GetMenu(MENU_FontSize);
- FailNIL(gFontSizeMenu, kErrInitialization);
- OpenAuditParameterFile();
- ReadAuditParameters();
- InitCursor();
- }
-
- /*
- * This displays an error message and its accompaning text. It should be
- * redone to use a message index (to an STR# resource.
- */
- void
- ErrorAlert(
- OSErr status,
- short messageIndex,
- Boolean fatal
- )
- {
- Str15 statusText;
- short alertID;
- Str255 errorMsg;
-
- NumToString(status, statusText);
- GetIndString(errorMsg, STRN_Messages, messageIndex);
- alertID = (fatal) ? ALRT_Fatal : ALRT_NonFatal;
- ParamText(statusText, errorMsg, "\p", "\p");
- StopAlert(alertID, NULL);
- }
-
-